package com.miao.teacher.widget;

import java.io.IOException;
import java.util.Date;

import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Handler;
import android.text.format.DateFormat;
import android.util.Log;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

public class SeekBarPlayer {
	private static final String TAG = "Player";

	private static Handler handler;

	private MediaPlayer mediaPlayer = null;
	private String file;
	private boolean isReady;
	private SeekBar seekBar = null;
	private TextView curTimeView = null;
	private TextView tolTimeView = null;
	private Runnable runnable = null;

	private int max=0;
	//private boolean IsStart=false;
	public static final int SEAK_FB_TIME = 5000;
	
	public static void setHandler(Handler h) {
		handler = h;
	}

	public static SeekBarPlayer create(String f) {
		SeekBarPlayer re = new SeekBarPlayer(f);
		if (re.isReady())
			return re;
		return null;
	}

	public static SeekBarPlayer create(String f, SeekBar s, TextView c, TextView t) {
		SeekBarPlayer re = new SeekBarPlayer(f);
		if (re.isReady()) {
			re.setSeekBar(s, c, t);
			return re;
		}
		return null;
	}
	public int getDur(String path) {
		if (mediaPlayer != null) {
			try {
				if (mediaPlayer != null && mediaPlayer.isPlaying()) {
					mediaPlayer.stop();
					mediaPlayer.release();
					mediaPlayer = null;
				}
				mediaPlayer = new MediaPlayer();
				mediaPlayer.setDataSource(path);
				mediaPlayer.prepare();
			} catch (Exception er) {
			}
			return mediaPlayer.getDuration() / 1000;
		}
		return 0;
	}
	public void setView(SeekBar s, TextView c, TextView t)
	{
		seekBar=s;
		curTimeView=c;
		tolTimeView=t;
		tolTimeView.setText(DateFormat.format("mm:ss",
				new Date(max)));
		if (seekBar == null)
			Log.i(TAG, "seekBar=null");
		seekBar.setMax(max);
	}
	private MediaPlayer getMediaPlayer()
	{
		return mediaPlayer;
	}
	public static SeekBarPlayer create(String f, SeekBar s) {
		SeekBarPlayer re = new SeekBarPlayer(f);
		if (re.isReady()) {
			re.setSeekBar(s);
			return re;
		}
		return null;
	}

	private SeekBarPlayer(final String f) {
		/*new Thread(){
			@Override
			public void run() {
				// TODO Auto-generated method stub
				super.run();*/
				file = f;
				isReady = false;
				mediaPlayer = new MediaPlayer();
				try {
					mediaPlayer.setDataSource(file);
					mediaPlayer.prepare();
					isReady = true;
				} catch (IllegalArgumentException e) {
					isReady = false;
					e.printStackTrace();
				} catch (SecurityException e) {
					isReady = false;
					e.printStackTrace();
				} catch (IllegalStateException e) {
					isReady = false;
					e.printStackTrace();
				} catch (IOException e) {
					isReady = false;
					e.printStackTrace();
				}
			/*}*/
			
		/*}.start();*/
	}

	public boolean isReady() {
		return isReady;
	}

	public boolean isPlaying() {
		return mediaPlayer.isPlaying();
	}

	public int getDuration() {
		return mediaPlayer.getDuration();
	}

	public int getCurrentPosition() {
		return mediaPlayer.getCurrentPosition();
	}

	public void play() {
		/*if(!IsStart)
		{
			seekBar.setProgress(0);
			mediaPlayer.seekTo(0);
			IsStart=true;
		}*/
		mediaPlayer.start();
	}

	public void pause() {
		mediaPlayer.pause();
	}

	public void seekTo(int seekto) {
		mediaPlayer.seekTo(seekto);
	}

	public void forward() {
		int currentPosition = mediaPlayer.getCurrentPosition();
		if (currentPosition + SEAK_FB_TIME <= mediaPlayer.getDuration()) {
			mediaPlayer.seekTo(currentPosition + SEAK_FB_TIME);
		} else {
			mediaPlayer.seekTo(mediaPlayer.getDuration() - 1);
		}
	}

	public void backward() {
		int currentPosition = mediaPlayer.getCurrentPosition();
		if (currentPosition -SEAK_FB_TIME >= 0) {
			mediaPlayer.seekTo(currentPosition - SEAK_FB_TIME);
		} else {
			mediaPlayer.seekTo(0);
		}
	}

	public void setOnCompletionListener(OnCompletionListener listener) {
		mediaPlayer.setOnCompletionListener(listener);
	}

	public void setSeekBar(SeekBar _seekBar, TextView curTextView,
			TextView tolTextView) {
		if (!isReady)
			return;
		seekBar = _seekBar;
		curTimeView = curTextView;
		tolTimeView = tolTextView;
		tolTimeView.setText(DateFormat.format("mm:ss",
				new Date(mediaPlayer.getDuration())));
		if (seekBar == null)
			Log.i(TAG, "seekBar=null");
		seekBar.setMax(mediaPlayer.getDuration());
		max=mediaPlayer.getDuration();
		//seekBar.setProgress(mediaPlayer.getDuration()/2);
		//seekBar.setProgress(mediaPlayer.getDuration()/2);
		runnable = new Runnable() {
			@Override
			public void run() {
				// Log.i(TAG,
				// "getCurrentPosition = "
				// + mediaPlayer.getCurrentPosition());
				if (mediaPlayer == null)
					Log.i(TAG, "bla mediaPlayer = null");
				if (curTimeView != null) {
					curTimeView.setText(DateFormat.format("mm:ss", new Date(
							mediaPlayer.getCurrentPosition())));
				}
				if (tolTimeView != null) {
				}
				if (seekBar != null) {
					/*if(!IsStart)
					{
						
					}else{*/
						seekBar.setProgress(mediaPlayer.getCurrentPosition());
					/*}*/
				} else {
					Log.i(TAG, "seekbar = null");
				}
				handler.postDelayed(runnable, 100);
			}
		};
		seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
			}

			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
			}

			@Override
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				if (!fromUser)
					return;
				if (mediaPlayer != null) {
					if (isReady) {
						Log.d(TAG, "seekto " + mediaPlayer.getCurrentPosition());
						mediaPlayer.seekTo(progress);
					}
				}
			}
		});
		Log.i(TAG, "postDelayed(runnable, 100)");
		handler.postDelayed(runnable, 100);
	}

	public void setSeekBar(SeekBar _seekBar) {
		if (!isReady)
			return;
		seekBar = _seekBar;
		tolTimeView = null;
		curTimeView = null;
		if (seekBar == null)
			Log.i(TAG, "seekBar=null");
		seekBar.setMax(mediaPlayer.getDuration());
		runnable = new Runnable() {
			@Override
			public void run() {
				// Log.i(TAG,
				// "getCurrentPosition = "
				// + mediaPlayer.getCurrentPosition());
				if (mediaPlayer == null)
					Log.i(TAG, "bla mediaPlayer = null");
				if (curTimeView != null) {
					curTimeView.setText(mediaPlayer.getCurrentPosition());
				}
				if (tolTimeView != null) {
					tolTimeView.setText(mediaPlayer.getCurrentPosition());
				}
				if (seekBar != null) {
					seekBar.setProgress(mediaPlayer.getCurrentPosition());
				} else {
					Log.i(TAG, "seekbar = null");
				}
				handler.postDelayed(runnable, 100);
			}
		};
		seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
			}

			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
			}

			@Override
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				if (!fromUser)
					return;
				if (mediaPlayer != null) {
					if (isReady) {
						Log.d(TAG, "seekto " + mediaPlayer.getCurrentPosition());
						mediaPlayer.seekTo(progress);
					}
				}
			}
		});
		Log.i(TAG, "postDelayed(runnable, 100)");
		handler.postDelayed(runnable, 100);
	}

	public void release() {
		handler.removeCallbacks(runnable);
		mediaPlayer.release();
		mediaPlayer = null;
		Log.e(TAG, "==== Player.release()");
	}
}